//logs.js
var utils = require("../../utils/appUtils.js")
var app=getApp();
var network = require("../../utils/NetWorkUtils.js")
Page({
  /**
   * 页面的初始数据
   */
  data: {
    isshowDialog: true,
    nikeName:'',//用户名
    headImg:'',//用户头像
    goldNum:0,//金币数量
    advertisementBanner:'',//底部banner图片
    jump_object:'',//底部banner跳转地址
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    // 检查是否授权
    wx.getSetting({
      success(e) {
        console.log(e)
        //如果已经授权
        if (e.authSetting['scope.userInfo']) {
          //授权隐藏弹窗
          that.setData({
            // isshowDialog: true
          })
        } else {
          //未授权显示弹窗
          that.setData({
            // isshowDialog: false
          })
        }
      }
    })
    try {
      const value = wx.getStorageSync('userInfo')
      console.log(value.headImage)
      console.log(value.openId)
      console.log(value.nickName)

      if (value) {
        app.globalData.openId = value.openId;
        app.globalData.userNmae = value.nickName;
        app.globalData.avatarUrl = value.headImage;
        app.globalData.token=value.token
      }
    } catch (e) {
      // Do something when catch error
    }
  //设置用户头像和姓名
  this.setData({
    nikeName: app.globalData.userNmae,
    headImg: app.globalData.avatarUrl
  })

    this.requestButtomBanner();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    // 检查授权是否过期
    wx.checkSession({
      success: function (e) {
        that.setData({
          isshowDialog: true
        })
      },
      fail: function (e) {
        that.setData({
          isshowDialog: false
        })
      }
    })
    //调用我的金币接口
    that.getGold(that)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    wx.showShareMenu({
      withShareTicket: true
    })
    return {
      title: '我正在答题赚钱，免费兑换商品，快来和我一起抢！',
      path: 'pages/index/index?openid=' + app.globalData.openId,
      imageUrl: '../../images/share_pic.jpg',
    }
  },

  // 跳转兑换订单
  jump_order(){
    wx.navigateTo({
      url: '../mine/ExchangeOrder',
    })
  },
  //跳转收货地址
  jump_address(){
    wx.navigateTo({
      url: '../mine/ReceivingAddress',
    })
  },

  //邀请好友
  inviteFriend: function () {
     console.log('分享')
    wx.showShareMenu({
      withShareTicket: true
    })
    return {
      title: '我正在答题赚钱，免费兑换商品，快来和我一起抢！',
      path: 'pages/index/index',
      imageUrl: '../../images/share_pic.jpg',
    }
    // this.onShareAppMessage();
  },

  //跳转排行榜
  jump_rank(){
    wx.navigateTo({
      url: '../mine/RankList',
    })
  },
  //跳转玩转攻略
  jump_strategy(){
    wx.navigateTo({
      url: '../webView/indexWebView?bannerUrl=https://www.jianxiongxcx.com/api/web/contact/id/2'
    })
  },
  jump_gold_detail(){
    wx.navigateTo({
      url: '../mine/GoldDetail',
    })
  },

  //联系我们跳转
  jum_about(){
    wx.navigateTo({
      url: '../webView/indexWebView?bannerUrl=https://www.jianxiongxcx.com/api/web/contact/id/1'
    })
  },
  //点击允许授权后的操作
  onGotUserInfo(e) {
    let userInfo = e.detail.userInfo;
    var that = this;
    console.log(e)
    if (e.detail.userInfo) {
      //调用公共授权登录方法
      network.resetLogin(0,0,userInfo, function () {
        that.setData({
          isshowDialog: true
        })
        that.onLoad();
        that.getGold(that)
      })
      // wx.login({
      //   success: function (res) {
      //     console.log(res);
      //     if (res.code) {
      //       //参数
      //       let dataParams = {
      //         code: res.code
      //       }
      //       //授权后请求获取openid接口
      //       network.requestHttp('m=api&c=Index&a=getopenid', dataParams,
      //         function (res) {
      //           console.log(res);
      //           that.setData({
      //             isshowDialog: true
      //           })

      //           //登录成功设置全局公共参数变量
      //           app.globalData.openId = res.data.data.openid;
      //           app.globalData.userNmae = userInfo.avatarUrl;
      //           app.globalData.avatarUrl = userInfo.nickName;
      //           app.globalData.token = utils.md5(res.data.data.openid + res.data.data.timeout + 'lsj123')

      //           var saveUserInfo = {};
      //           saveUserInfo['openId'] = res.data.data.openid;
      //           saveUserInfo['headImage'] = userInfo.avatarUrl;
      //           saveUserInfo['nickName'] = userInfo.nickName;
      //           saveUserInfo['token'] = utils.md5(res.data.data.openid + res.data.data.timeout + 'lsj123')
      //           console.log(res.data.data.openid);
      //           wx.setStorageSync('userInfo', saveUserInfo);
      //           //参数
      //           var dataParams = {
      //             // openid: userInfo.openid,
      //             headimgurl: userInfo.avatarUrl,
      //             nickname: userInfo.nickName
      //           }
      //           //请求登录接口
      //           network.requestHttp('m=api&c=Index&a=login', dataParams,
      //             function (res) {
      //               console.log(res)
      //               //请求成功设置全部变量
      //              that.onLoad();
      //               //调用我的金币接口
      //               that.getGold(that)
      //             })
      //         })
      //     }
      //   }
      // })
    } else {
      wx.showModal({
        title: '警告',
        content: '您点击了拒绝授权，将无法使用小程序，请允许授权!!!',
        showCancel: false,
        confirmText: '返回授权',
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击了“返回授权”')
          }
        }
      })
    }
  },

  //请求我的金币接口
  getGold:function(that){
    if (app.globalData.openId.length == 0 || app.globalData.token.length==0){
      return
    }
    //网络请求参数
    var requestData = {
      // openid: app.globalData.openId,
      // logincode: app.globalData.token
    }
    console.log(app.globalData.openId)
    //请求我的页面数据
    network.requestHttp("m=api&c=Index&a=main", requestData, function (data) {
      console.log(data)
      //设置我的金币
      that.setData({
        goldNum: data.data.data.coin
      })
    })
  },

  //请求底部banner图片
requestButtomBanner: function () {
    var that = this;
    var requestData = { position: 5 }
    network.requestHttp("m=api&c=Index&a=banner", requestData, function (res) {
      console.log(res)
      that.setData({
        advertisementBanner: res.data.data.pic,
        jump_object: res.data.data
      })
    })
  },

  //底部banner跳转
  banner_jump() {
    var dataParam = {
      id: 5
    }
    network.requestHttp("m=api&c=Index&a=clicked", dataParam, function (res) {

    })
    var that = this;
   

    if (that.data.jump_object.jump == '2') {
      wx.navigateToMiniProgram({
        appId: that.data.jump_object.appid,
        path: that.data.jump_object.path
      })
    } else {
      wx.navigateTo({
        url: '../webView/indexWebView?bannerUrl=' + that.data.jump_object.jump_url
      })
    }
  },
})
